package com.example.xiazai.fragment;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.xiazai.MyService;
import com.example.xiazai.R;
import com.example.xiazai.bean.ProMsg;
import com.example.xiazai.zidingyiview.CircleProgressBar;
import com.github.dfqin.grantor.PermissionListener;
import com.github.dfqin.grantor.PermissionsUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class XiaZaiFragment extends Fragment {
    @BindView(R.id.pb)
    ProgressBar mPb;
    @BindView(R.id.txt)
    TextView mTxt;
    @BindView(R.id.btn_xiazai)
    Button mBtnXiazai;
    @BindView(R.id.cpb)
    CircleProgressBar mCpb;
    private View view;
    private Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_xiazai, null);
        initPermiss();
        initView();
        unbinder = ButterKnife.bind(this, inflate);
        EventBus.getDefault().register(this);
        return inflate;
    }

    private void initView() {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getProMsg(ProMsg proMsg) {
        if (proMsg.getType() == 1) {//表示设置最大进度
            mPb.setMax(proMsg.getMax());
        } else if (proMsg.getType() == 2) {
            mCpb.setProgress(proMsg.getTxt());
            mPb.setProgress(proMsg.getProgress());
            mTxt.setText(+proMsg.getTxt() + "%");
        }
    }

    private void initPermiss() {
        PermissionsUtil.requestPermission(getActivity(), new PermissionListener() {
            @Override
            public void permissionGranted(@NonNull String[] permission) {
                Toast.makeText(getActivity(), "申请权限成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void permissionDenied(@NonNull String[] permission) {
                Toast.makeText(getActivity(), "申请权限失败", Toast.LENGTH_SHORT).show();
            }
        }, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE});
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.btn_xiazai)
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.btn_xiazai:
                Intent intent = new Intent(getActivity(), MyService.class);
                getActivity().startService(intent);
                break;
        }
    }
}
